/@Author: Omar Aly
/@Author: Nourhan Aloush
/@Buttons:  1. if the offer is not published, a "Publish Now" button will appear
/           =>  on clicking it, it calls publish in the controller
/           2. if the offer is not activated, and the number of subscribed user > min quantity
/           => on clicking it, calls activate in the controller
/           3. if the offer has expired, extend offer button will apear
/           => on clicking it, it redirects the user to the edit template of the offer
/           4. if the offer is activated, input transaction code link will apear
/           => on clicking it, redirects the user to be able to input the transaction code in another template.
/           5. delete offer is available if the offer is not activated yet, 
/           => on clicking it, deletes the offer and all the transactions relevant to it. 

#flash
  %div{:style => "width:400px"}
    - if flash[:notice]
      .alert.alert-success
        =flash[:notice]
    - if flash[:error]
      .alert.alert-error
        =flash[:error]

-if current_supplier != nil
  -session[:id] = current_supplier.id

-if supplier_signed_in? 
  -if (User.find(session[:id]))._type == 'Supplier'
    %h1 My Offers
    %br
    %br
    %p{style:"font-size:20px;"}
    -if !@Offers.empty?
      = will_paginate @Offers, :container => false
      - @Offers.each do |offer|
        .well
          .row-fluid
            .span6-fluid{:style => "text-align:left"}
              %h2= "Offer Placed On: "
            .span6-fluid{:style => "text-align:left"}
              %h2=link_to offer.need.name, supplier_need_path(offer.need.id.to_s)
              %br
          .row-fluid
            .span2
              =image_tag offer.need.photo.url(:normal),:width =>'80px',:height =>'80px'
            .span6{:style => "text-align:left"}
              %h3= "More information about this offer: "
              %p
                =offer.description
            .span4{:style => "text-align:right"}
              %h3= "Subscribed:" + " #{offer.num_of_subscribed_quantity}/#{offer.quantity}"
              %h3= "Price: " + offer.price.to_s + "$"
              %h3= "Expires on: " + offer.expiry_date.to_s
          .row-fluid
            -if offer.num_of_subscribed_quantity>= offer.min_quantity
              .progress.progress-info.progress-striped.active
                .bar{:style => "width: #{offer.num_of_subscribed_quantity*100/offer.quantity}%;"}
            -else
              .progress.progress-danger.progress-striped.active
                .bar{:style => "width: #{offer.num_of_subscribed_quantity*100/offer.quantity}%;"}
          .row-fluid
            -if offer.activated
              .span12{:style => "text-align:center"}
                =link_to "Input transaction code" , insert_code_supplier_transactions_path(offer.id)
            -else
              -if !offer.expired
                -if offer.published
                  -if offer.num_of_subscribed_quantity>=offer.min_quantity
                    .span6{:style => "text-align:center"}
                      =button_to "Activate Now!" ,  activate_supplier_offers_path(:id => offer.id), :class=>"btn-large  btn-success", :confirm => "Are you sure you want to activate this offer?"
                  -else
                    .span6{:style => "text-align:center"}
                      .alert.alert-error
                        ="Waiting for more subscribers"
                -else
                  .span6{:style => "text-align:center"}
                    =form_tag offer,:method => 'get' do
                      =hidden_field_tag :id,offer.id
                      =submit_tag 'Publish Now!', :class=>"btn-large  btn-success"
	                                  
                .span6{:style => "text-align:center"}
                  =button_to "Delete Offer" , updatedelete_supplier_offers_path(:offer_id => offer.id), :class=>"btn-large  btn-danger" , :confirm => "Are you sure you want to delete this offer?"
              -else
                .span6{:style => "text-align:center"}
                  %h5= "Your offer has expired, Do you want to extend it?"
                  =button_to "Extend Offer" , edit_offer_path(:id => offer.id), method: :get , :class=>"btn-large  btn-success", :confirm => "You will now be redirected to edit your offer and start it again, are you sure you want to proceed?"
                .span6{:style => "text-align:center"}
                  %br
                  =button_to "Delete Offer" , updatedelete_supplier_offers_path(:offer_id => offer.id), :class=>"btn-large  btn-danger", :confirm => "Are you sure you want to delete this offer?"
    -else
      %div{:style => "width:290px"}
        .alert.alert-error
          ="You don't have any offers yet!"
      =button_to "START HERE !!!", needs_path, :method => 'get', :class=>"btn-large  btn-success" 

-else
  = "You are not logged in as a supplier, please try to log in again"


